package com.chixing.config;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    /**
     * 订单队列
     *
     * @return {@link Queue}
     */
    @Bean
    public Queue orderQueue() {
        Map<String, Object> argments = new HashMap<>();
        argments.put("x-message-ttl", 120000);
        argments.put("x-dead-letter-exchange", "deadExchange");
        argments.put("x-dead-letter-routing-key", "deadKey");
        Queue queue = new Queue("orderQueue", true, false, false, argments);
        return queue;
    }

    /**
     * 订单交换机
     *
     * @return {@link Exchange}
     */
    @Bean
    public Exchange orderExchange() {
        return new DirectExchange("orderExchange", true, false, null);
    }

    /**
     * 订单路由键
     *
     * @return {@link Binding}
     */
    @Bean
    public Binding orderRouting() {
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with("routingKey").noargs();
    }

    /**
     * 订单死信队列
     *
     * @return {@link Queue}
     */
    @Bean
    public Queue orderDlxQueue() {
        Queue queue = new Queue("deadQueue", true, false, false);
        return queue;
    }

    /**
     * 订单死信交换机
     *
     * @return {@link Exchange}
     */
    @Bean
    public Exchange orderDlxExchange() {
        return new DirectExchange("deadExchange", true, false, null);
    }

    /**
     * 订单死信路由键
     *
     * @return {@link Binding}
     */
    @Bean
    public Binding orderDlxRouting() {
        return BindingBuilder.bind(orderDlxQueue()).to(orderDlxExchange()).with("deadKey").noargs();
    }

}
